/**
 * 用户模块
 */
import { defineStore } from 'pinia'
import { StoreName } from '../store-enum.d'
import { ref } from 'vue'

/** 用户信息类型 */
interface UserInfo {
  id: string
  avatar: string
  nickname: string
  account: string
  mobile: string
  token: string
}

export const useUserStore = defineStore(
  StoreName.USER,
  () => {
    /** 用户信息 */
    const userInfo = ref<UserInfo>({
      id: '',
      avatar: '',
      nickname: '',
      account: '',
      mobile: '',
      token: ''
    })

    /** 设置用户信息 */
    const setUserInfo = (payload: UserInfo): void => {
      userInfo.value = payload
    }

    return {
      userInfo,
      setUserInfo
    }
  },
  /** 持久化存储，默认存储到localstorage */
  {
    persist: {
      key: 'user-store' // 存储到localstorage中的key名称
    }
  }
)
